﻿using System.Data.Objects;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using GENERIC.Repository.Tests.Models;
using GENERIC.Respository;
using GENERIC.Respository.Interfaces;

namespace GENERIC.EFRepository.Tests
{
    [TestClass]
    public class TestsEF
    {

        [TestMethod]
        public void TestEF()
        {
            Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
            Database.SetInitializer(new DropCreateDatabaseAlways<EFContext>());
            var context = new EFContext("TestContext", string.Empty, GetType().Assembly);

            var customerRepository = new Repository<Customer>(context.CreateSet<Customer>());
            var orderRepository = new Repository<Order>(context.CreateSet<Order>());

            //Insértion des données
            customerRepository.Insert(new Customer() { Id = 1, Name = "Dupont" });
            customerRepository.Insert(new Customer() { Id = 2, Name = "Durand" });
            context.SaveChanges();


            //Enregistrement n'existe pas
            Assert.AreEqual(customerRepository.Get(i => i.Id == 0).FirstOrDefault(), null);
            //enregistrement unique
            Assert.AreEqual(customerRepository.Get(i => i.Id == 1).Single().Name, "Dupont");
            //pas d'enregistrement dans la table
            Assert.AreEqual(orderRepository.Get().Count(), 0);

            orderRepository.Insert(new Order() { Id = 1, CustumerId = 2, Price = 10 });
            context.SaveChanges();

            var order = orderRepository.Get(i => i.Price == 10).SingleOrDefault();
            //Enregistrmement existe
            Assert.AreNotEqual(order, null);
            //Update teste
            order.Price = 20;
            orderRepository.Update(order);
            context.SaveChanges();

            order = orderRepository.Get(i => i.Price == 20).SingleOrDefault();
            Assert.AreNotEqual(order, null);
            //Delete
            order = orderRepository.Get().First();
            orderRepository.Delete(order);
            context.SaveChanges();
            Assert.AreEqual(orderRepository.Get().Count(), 0);
        }
    }

    
}
